package com.matt.service.care.impl;


import com.matt.domain.care.HLHandoverRecord;
import com.matt.domain.care.HLHandoverRecordMedicine;
import com.matt.foundation.model.DailyRange;
import com.matt.repository.care.HLHandoverRecordMedicineRepository;
import com.matt.repository.care.HLHandoverRecordRepository;
import com.matt.service.care.HLHandoverRecordService;
import com.matt.service.care.po.HLHandoverRecordMedicinePO;
import com.matt.service.care.po.HLHandoverRecordPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
@Slf4j
public class HLHandoverRecordServiceImpl extends HLCareBaseServiceImpl<HLHandoverRecord, HLHandoverRecordRepository, HLHandoverRecordPO> implements HLHandoverRecordService {

	@Autowired
	@Override
	public void setBaseRepository(HLHandoverRecordRepository repository){
		super.setBaseRepository(repository);
	}

	@Autowired
	private HLHandoverRecordMedicineRepository medicineRepository;

	@Override
	public List<HLHandoverRecord> addAll(String areaId, String staffId, HLHandoverRecordPO param){
		return null;
	}

	@Override
	public Page<HLHandoverRecord> getList(String areaId, String staffId, String childId,Date daily, PageRequest pageRequest) {
		Specification<HLHandoverRecord> specification = new Specification<HLHandoverRecord>() {
			@Override
			public Predicate toPredicate(Root<HLHandoverRecord> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicates = new ArrayList<>();

				DailyRange dr = DailyRange.of(daily);

				Predicate p2 = criteriaBuilder.and(criteriaBuilder.equal(root.get("areaId"), areaId),criteriaBuilder.equal(root.get("deleted"), 0));
				Predicate p3 = criteriaBuilder.between(root.get("eventTime"), dr.getDayBegin(), dr.getDayEnd());
				query.where(p2);
				if (Objects.nonNull(childId)){
					Predicate p1 = criteriaBuilder.and(criteriaBuilder.equal(root.get("childId"), childId));
					Predicate p = criteriaBuilder.and(p1, p2,p3);
					query.where(p);
				}else {
					Predicate p = criteriaBuilder.and( p2,p3);
					query.where(p);
				}
				query.orderBy(criteriaBuilder.desc(root.get("created")));
				return query.getRestriction();
			}
		};
		Page<HLHandoverRecord> page = baseRepository.findAll(specification, pageRequest);

		return page;
	}



	@Override
	@Transactional(rollbackOn = RuntimeException.class)
	public HLHandoverRecord add(String areaId, String staffId, HLHandoverRecordPO param){
		List<HLHandoverRecordMedicinePO> medicines = param.getMedicines();
		param.setMedicines(null);
		HLHandoverRecord ret = super.add(areaId, staffId, param);

		if (medicines == null){
			return ret;
		} else {
			if (StringUtils.isNotBlank(param.getId())){
				medicineRepository.deleteAllByHandoverRecord(ret);
			}
			List<HLHandoverRecordMedicine> batch = new ArrayList<>();
			for (HLHandoverRecordMedicinePO medicinePO : medicines) {
				HLHandoverRecordMedicine m = new HLHandoverRecordMedicine();

				BeanUtils.copyProperties(medicinePO, m, "id");

				m.setCreatedBy(staffId);
				m.setCreated(new Date());
				m.setHandoverRecord(ret);

				batch.add(m);
			}

			medicineRepository.saveAll(batch);
		}

		return ret;
	};

}
